package com.idealighter.game.gm.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.idealighter.game.backpack.manager.BackpackMgr;
import com.idealighter.game.blacklist.manager.BlackListMgr;
import com.idealighter.game.chat.manager.ChatMsgMgr;
import com.idealighter.game.core.common.Game;
import com.idealighter.game.core.constant.notice.Notice;
import com.idealighter.game.core.constant.notice.NoticeType;
import com.idealighter.game.core.util.PwdEncode;
import com.idealighter.game.dblog.service.DbLogService;
import com.idealighter.game.dictionary.dic.SysconfigDic;
import com.idealighter.game.dictionary.dic.WelfareDic;
import com.idealighter.game.event.manager.EventMgr;
import com.idealighter.game.event.struct.ClearGameDataEvent;
import com.idealighter.game.event.struct.ShutdownRoomEvent;
import com.idealighter.game.event.struct.StartRoomEvent;
import com.idealighter.game.games.common.AbstractRoom;
import com.idealighter.game.gm.struct.BackendOrder;
import com.idealighter.game.gm.struct.ControlPlayer;
import com.idealighter.game.log.core.LogReason;
import com.idealighter.game.log.struct.gm.BackendAddGoldLog;
import com.idealighter.game.log.struct.gm.BackendOrderLog;
import com.idealighter.game.notice.manager.NoticeMsgMgr;
import com.idealighter.game.player.manager.PlayerMgr;
import com.idealighter.game.player.service.bo.PlayerBo;
import com.idealighter.game.player.struct.Player;
import com.idealighter.game.prizepool.manager.PlayerPrizePoolMgrScript;
import com.idealighter.game.prizepool.manager.RoomPrizePoolMgrScript;
import com.idealighter.game.web.core.HttpRes;
import com.idealighter.game.web.core.WebHandler;
import com.idealighter.game.web.player.controller.convert.PlayerDtoConvert;
import com.idealighter.game.web.player.controller.dto.OnLinePlayerDto;
import com.idealighter.game.whitelist.manager.WhiteListMgr;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 后台命令管理，带 . Script是可以热更新的,最好不要有状态
 * 
 * @date 2015年8月25日 上午11:14:21
 *
 */
@Singleton
public class BackendOrderMgrScript implements WebHandler {

  private static final Logger LOG = LoggerFactory.getLogger("GMLog");
  public static final ScriptEngine NASHORN = new ScriptEngineManager().getEngineByName("nashorn");

  private static final Logger RECHARGE_LOG = LoggerFactory.getLogger("RechargeLog");

  private static final int SUCCESS = 1;
  private static final int FAILURE = 0;

  @Inject
  private PlayerMgr playerMgr;
  @Inject
  private PlayerPrizePoolMgrScript playerPrizePoolMgr;
  @Inject
  private RoomPrizePoolMgrScript roomPrizePoolMgr;
  @Inject
  private BlackListMgr blackListMgr;
  @Inject
  private WhiteListMgr whiteListMgr;
  @Inject
  private WelfareDic welfareDic;
  @Inject
  private SysconfigDic sysconfigDic;
  @Inject
  private NoticeMsgMgr noticeMsgMgr;
  @Inject
  private ChatMsgMgr chatMsgMgr;
  @Inject
  private BackpackMgr backpackMgr;

  @Inject
  private EventMgr eventMgr;

  @Override
  public HttpRes handle(String ip, String content) {
    BackendOrder order = JSON.parseObject(content, BackendOrder.class);
    // 后台gm命令
    String cmd = order.getCmd();
    // 后台命令参数
    List<String> args = order.getArgs();

    if (cmd == null || cmd.trim().equals("")) {
      LOG.error("请输入正确的gm指令");
      return new HttpRes(FAILURE, "请输入正确的gm指令", "");
    }

    LOG.info("收到后台[{}]gm命令[{}]", ip, content);

    // gm执行结果消息
    HttpRes res = null;

    try {
      // 方法类型
      MethodType methodType = MethodType.methodType(HttpRes.class, List.class);
      // 方法调用器
      MethodHandle handle = MethodHandles.lookup().findVirtual(getClass(), cmd, methodType);
      res = (HttpRes) handle.invoke(this, args);
      DbLogService.log(new BackendOrderLog(ip, cmd, args.toString()));

    } catch (Throwable e) {
      LOG.error("gm指令[" + cmd + args.toString() + "]错误:", e);
      res = HttpRes.failure(e.getMessage());
    }

    return res;
  }

  /**
   * 获取在线玩家 .
   * <p>
   * . gameId . int 游戏id(-1所有,0:大厅,其他:游戏id)
   * </p>
   * .
   * <p>
   * . roomId . int 房间id(0:所有房间,其他:房间id)
   * </p>
   * .
   * <p>
   * . page . int
   * </p>
   * .
   * <p>
   * . size . int
   * </p>
   * .
   * <p>
   * . playerId . long
   * </p>
   * .
   * <p>
   * . erName String
   * </p>
   * .
   * 
   * 
   * @return .
   */
  public HttpRes onLinePlayers(List<String> args) {
    int gameId = Integer.parseInt(args.get(0));
    int roomId = Integer.parseInt(args.get(1));
    int page = Integer.parseInt(args.get(2));
    int size = Integer.parseInt(args.get(3));
    long playerId = Long.parseLong(args.get(4));
    String uerName = args.get(5);
    ConcurrentHashMap<Long, Player> allPlayers = playerMgr.onLinePlayers();

    // 每页最大行数
    int start = (page - 1) * size;
    int total = 0;
    List<OnLinePlayerDto> onLinePlayers = new ArrayList<>();

    for (Player player : allPlayers.values()) {
      if (!player.robot()) {

        if (playerId > 0 && player.getId() != playerId) {
          continue;
        }

        if (uerName != null && !player.getUserName().equals(uerName)) {
          continue;
        }

        AbstractRoom room = player.curRoom;
        if (gameId == 0 && room != null) {
          continue;
        }

        if (gameId > 0 && (room == null || room.game().getType() != gameId)) {
          continue;
        }

        if (roomId > 0
            && (room == null || room.game().getType() != gameId || room.getId() != roomId)) {
          continue;
        }

        if (total >= start && total < start + size) {
          OnLinePlayerDto dto = PlayerDtoConvert.INSTANCE.toOnLinePlayerDto(player);
          onLinePlayers.add(dto);
        }
        total++;
      }
    }

    JSONObject data = new JSONObject();
    data.put("total", total);
    data.put("onLinePlayers", onLinePlayers);

    return new HttpRes(SUCCESS, "", JSON.toJSONString(data));
  }

  /**
   * 获取在线玩家信息 .
   * <p>
   * . playerId . long
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes onLinePlayerInfo(List<String> args) {
    long playerId = Long.parseLong(args.get(0));
    Player player = playerMgr.getPlayer(playerId);

    if (player == null) {
      return new HttpRes(FAILURE, "该玩家不存在或者不在线", "");
    } else {
      return new HttpRes(SUCCESS, "", JSON.toJSONString(player));
    }
  }

  /**
   * 给玩家充值 .
   * <p>
   * . playerId .
   * </p>
   * .
   * <p>
   * . orderid .
   * </p>
   * .
   * <p>
   * . payway .
   * </p>
   * .
   * <p>
   * . paywayname .
   * </p>
   * .
   * <p>
   * . payamount .
   * </p>
   * .
   * <p>
   * . paygold .
   * </p>
   * .
   * <p>
   * . providerorderid .
   * </p>
   * .
   */
  public HttpRes recharge(List<String> args) {
    long playerId = Long.parseLong(args.get(0));
    String orderid = args.get(1);
    String payway = args.get(2);
    String paywayname = args.get(3);
    long payamount = Long.parseLong(args.get(4));
    long paygold = Long.parseLong(args.get(5));
    String providerorderid = args.get(6);

    if (playerMgr.recharge(playerId, orderid, payway, paywayname, payamount, paygold,
        providerorderid)) {
      RECHARGE_LOG.info("玩家[{}]充值元宝[{}]金币[{}]成功", playerId, payamount, paygold);
      return new HttpRes(SUCCESS, "充值成功", "");
    } else {
      RECHARGE_LOG.warn("玩家[{}]充值元宝[{}]金币[{}]失败", playerId, payamount, paygold);
      return new HttpRes(FAILURE, "用户对应的玩家不存在充值失败", "");
    }
  }

  /**
   * js脚本命令 .
   * 
   * @param args 参数.
   * @return 结果.
   * @throws ScriptException 执行异常.
   */
  public HttpRes js(List<String> args) throws ScriptException {
    NASHORN.eval(args.get(0));

    return new HttpRes(SUCCESS, "js脚本命令执行成功", "");
  }

  /**
   * 玩家状态 .
   * 
   * @return .
   */
  public HttpRes playerStatus(List<String> args) {
    String playerIdsStr = args.get(0);
    StringBuilder playerStatus = new StringBuilder(256);

    for (String playerIdStr : playerIdsStr.split(",")) {
      Player player = playerMgr.getPlayer(Long.parseLong(playerIdStr));
      if (player == null) {
        playerStatus.append(playerIdStr + "-离线,");
      } else {
        AbstractRoom room = player.curRoom;
        if (room == null) {
          playerStatus.append(playerIdStr + "-游戏大厅,");
        } else {
          playerStatus.append(playerIdStr + "-" + room.game().getDesc() + "-" + room.getName());
        }
      }
    }

    if (playerStatus.length() > 0) {
      playerStatus.deleteCharAt(playerStatus.length() - 1);
    }

    return new HttpRes(SUCCESS, "", playerStatus.toString());
  }

  /**
   * 获取控制玩家列表.
   * <p>
   * . game .
   * </p>
   * .
   * <p>
   * . roomId .
   * </p>
   * .
   * <p>
   * . page .
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes controlPlayers(List<String> args) {
    int gameId = Integer.parseInt(args.get(0));
    int roomId = Integer.parseInt(args.get(1));
    int page = Integer.parseInt(args.get(2));

    Game game = Game.getGame(gameId);
    // 每页最大行数
    int limit = 10;
    int start = (page - 1) * limit;
    int total = 0;
    List<ControlPlayer> controlPlayers = new ArrayList<>();

    if (game != null) { // game不为空则从在线玩家中找
      Map<Long, Player> players = playerMgr.onLinePlayers();
      for (Player player : players.values()) {
        if (player.robot()) {
          continue;
        }
        AbstractRoom room = player.curRoom;

        if (room != null && room.game() == game && (roomId == 0 || room.getId() == roomId)) {
          total++;
          if (total >= start && total < start + limit) {
            controlPlayers.add(new ControlPlayer(player));
          }
        }
      }
    }

    JSONObject data = new JSONObject();
    data.put("total", total);
    data.put("controlPlayers", controlPlayers);

    return new HttpRes(SUCCESS, "", JSON.toJSONString(data));
  }

  /**
   * 查找单个人.
   * <p>
   * . playerId .
   * </p>
   * .
   * <p>
   * . playerName .
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes controlPlayer(List<String> args) {
    int playerId = Integer.parseInt(args.get(0));
    String playerName = args.get(1);

    Collection<PlayerBo> activePlayers = playerMgr.activePlayers();
    if (playerId != 0) {
      for (PlayerBo domain : activePlayers) {
        if (domain.isRobot()) {
          continue;
        }
        if (domain.getId() == playerId) {
          Player player = playerMgr.getPlayer(playerId);
          if (player != null) {
            return new HttpRes(SUCCESS, "", JSON.toJSONString(new ControlPlayer(player)));
          } else {
            return new HttpRes(SUCCESS, "", JSON.toJSONString(new ControlPlayer(domain)));
          }
        }
      }
    } else if (playerName != null) {
      for (PlayerBo domain : activePlayers) {
        if (domain.isRobot()) {
          continue;
        }
        if (domain.getPlayerName().equals(playerName)) {
          Player player = playerMgr.getPlayer(playerId);
          if (player != null) {
            return new HttpRes(SUCCESS, "", JSON.toJSONString(new ControlPlayer(player)));
          } else {
            return new HttpRes(SUCCESS, "", JSON.toJSONString(new ControlPlayer(domain)));
          }
        }
      }
    }

    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 增加个人游戏奖池金币.
   * <p>
   * . playerId .
   * </p>
   * .
   * <p>
   * . gameId .
   * </p>
   * .
   * <p>
   * . gold .
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes addPlayerPrizePoolGold(List<String> args) {
    int playerId = Integer.parseInt(args.get(0));
    int gameId = Integer.parseInt(args.get(1));
    long gold = Long.parseLong(args.get(2));

    playerPrizePoolMgr.addPrizePoolGold(playerId, gameId, gold);
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 更新个人游戏奖池金额 .
   * <p>
   * . playerId .
   * </p>
   * .
   * <p>
   * . gameId .
   * </p>
   * .
   * <p>
   * . gold .
   * </p>
   * .
   */
  public HttpRes updatePlayerPrizePoolGold(List<String> args) {
    int playerId = Integer.parseInt(args.get(0));
    int gameId = Integer.parseInt(args.get(1));
    long gold = Long.parseLong(args.get(2));

    playerPrizePoolMgr.updatePrizePoolGold(playerId, gameId, gold);
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 更新个人游戏奖池控制.
   * <p>
   * . playerId .
   * </p>
   * .
   * <p>
   * . gameId .
   * </p>
   * .
   * <p>
   * . control .
   * </p>
   * .
   */
  public HttpRes updatePlayerPrizePoolControl(List<String> args) {
    int playerId = Integer.parseInt(args.get(0));
    int gameId = Integer.parseInt(args.get(1));
    String control = args.get(2);

    playerPrizePoolMgr.updatePrizePoolControl(playerId, gameId, control);
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 新增个人游戏奖池.
   * <p>
   * . playerId .
   * </p>
   * .
   * <p>
   * . gameId .
   * </p>
   * .
   * <p>
   * . gold .
   * </p>
   * .
   * <p>
   * . control .
   * </p>
   * .
   */
  public HttpRes insertPlayerPrizePoolControl(List<String> args) {
    int playerId = Integer.parseInt(args.get(0));
    int gameId = Integer.parseInt(args.get(1));
    long gold = Long.parseLong(args.get(2));
    String control = args.get(3);

    playerPrizePoolMgr.addPrizePool(playerId, gameId, gold, control);
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 获取房间奖池组.
   * <p>
   * . gameId .
   * </p>
   * .
   * <p>
   * . roomId .
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes roomPrizePoolGroup(List<String> args) {
    int gameId = Integer.parseInt(args.get(0));
    long roomId = Long.parseLong(args.get(1));
    Game game = Game.getGame(gameId);

    return new HttpRes(SUCCESS, "",
        JSON.toJSONString(roomPrizePoolMgr.getRoomPrizePoolGroup(game, roomId)));
  }

  /**
   * 修改房间奖池金额范围.
   * <p>
   * . gameId . int
   * </p>
   * .
   * <p>
   * . roomId . int
   * </p>
   * .
   * <p>
   * . poolIndex . int
   * </p>
   * .
   * <p>
   * . lower . long
   * </p>
   * .
   * <p>
   * . pper long
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes modifyPrizePoolRange(List<String> args) {
    int gameId = Integer.parseInt(args.get(0));
    int roomId = Integer.parseInt(args.get(1));
    int poolIndex = Integer.parseInt(args.get(2));
    long lower = Long.parseLong(args.get(3));
    long upper = Long.parseLong(args.get(4));

    roomPrizePoolMgr.modifyPrizePoolRange(Game.getGame(gameId), roomId, poolIndex, lower, upper);
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 修改房间奖池金额.
   * <p>
   * . gameId . int
   * </p>
   * .
   * <p>
   * . roomId . int
   * </p>
   * .
   * <p>
   * . poolIndex . int 负数表示默认奖池
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes delRoomPrizePool(List<String> args) {
    int gameId = Integer.parseInt(args.get(0));
    int roomId = Integer.parseInt(args.get(1));
    int poolIndex = Integer.parseInt(args.get(2));
    Game game = Game.getGame(gameId);

    if (poolIndex < 0) {
      roomPrizePoolMgr.delDefaultRoomPrizePool(game, roomId);
    } else {
      roomPrizePoolMgr.delGoldRangeRoomPrizePool(game, roomId, poolIndex);
    }

    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 删除房间控制策略.
   * <p>
   * . gameId . int
   * </p>
   * .
   * <p>
   * . roomId . int
   * </p>
   * .
   * <p>
   * . poolIndex . int 负数表示默认奖池
   * </p>
   * .
   * <p>
   * . strategyIndex . int
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes delRoomPrizePoolStrategy(List<String> args) {
    int gameId = Integer.parseInt(args.get(0));
    int roomId = Integer.parseInt(args.get(1));
    int poolIndex = Integer.parseInt(args.get(2));
    int strategyIndex = Integer.parseInt(args.get(3));
    Game game = Game.getGame(gameId);

    if (poolIndex < 0) {
      roomPrizePoolMgr.delDefaultRoomPrizePoolStrategy(game, roomId, strategyIndex);
    } else {
      roomPrizePoolMgr.delGoldRangeRoomPrizePoolStrategy(game, roomId, poolIndex, strategyIndex);
    }

    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 修改房间控制策略.
   * <p>
   * . gameId . int
   * </p>
   * .
   * <p>
   * . roomId . int
   * </p>
   * .
   * <p>
   * . poolIndex . int 负数表示默认奖池
   * </p>
   * .
   * <p>
   * . strategyIndex . int
   * </p>
   * .
   * <p>
   * . strategy . String
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes modifyRoomPrizePoolStrategy(List<String> args) {
    int gameId = Integer.parseInt(args.get(0));
    int roomId = Integer.parseInt(args.get(1));
    int poolIndex = Integer.parseInt(args.get(2));
    int strategyIndex = Integer.parseInt(args.get(3));
    String strategy = args.get(4);
    Game game = Game.getGame(gameId);

    if (poolIndex < 0) {
      roomPrizePoolMgr.modifyDefaultRoomPrizePoolStrategy(game, roomId, strategyIndex, strategy);
    } else {
      roomPrizePoolMgr.modifyGoldRangeRoomPrizePoolStrategy(game, roomId, poolIndex,
          strategyIndex, strategy);
    }

    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 修改用户数据.
   * <p>
   * . playerId . long
   * </p>
   * .
   * <p>
   * . name . String name,password,idcard,weixin,phone,email,validatePhone,validateEmail,bankPwd
   * </p>
   * .
   * <p>
   * . val . String
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes modifyUserData(List<String> args) {
    long playerId = Long.parseLong(args.get(0));
    String name = args.get(1);
    String val = args.get(2);

    PlayerBo playerDomain = playerMgr.selectPlayer(playerId);
    if (playerDomain == null) {
      return new HttpRes(FAILURE, "玩家不存在", "");
    }

    switch (name) {
      case "name":
        playerDomain.setName(val);
        break;
      case "password":
        playerDomain.setPassword(PwdEncode.encodeWithTail(val));
        break;
      case "idcard":
        playerDomain.setIdcard(val);
        break;
      case "weixin":
        playerDomain.setWeixin(val);
        break;
      case "phone":
        playerDomain.setPhone(val);
        break;
      case "email":
        playerDomain.setEmail(val);
        break;
      case "validatePhone":
        playerDomain.setValidatePhone(Boolean.parseBoolean(val));
        break;
      case "validateEmail":
        playerDomain.setValidateEmail(Boolean.parseBoolean(val));
        break;
      case "bankPwd":
        playerDomain.setBankPwd(PwdEncode.encodeWithTail(val));
        break;
      case "playerName":
        playerDomain.setPlayerName(val);
        break;
      case "userName":
        playerDomain.setUserName(val);
        break;
      default:
        return new HttpRes(FAILURE, "不支持的修改属性" + name, "");
    }

    // 立即同步保存一次
    playerMgr.updatePlayer(playerDomain);
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 冻结玩家 .
   * 
   * @param args . 就是要冻结的玩家列表
   * @return .
   */
  public HttpRes lockPlayer(List<String> args) {
    for (String playerIdStr : args) {
      long playerId = Long.parseLong(playerIdStr);
      PlayerBo playerDom = blackListMgr.lockPlayer(playerId);
      // 立即更新,后台实时看到结果
      playerMgr.updatePlayer(playerDom);
    }

    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 解冻玩家.
   * <p>
   * . playerId . long
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes unLockPlayer(List<String> args) {

    for (String playerIdStr : args) {
      long playerId = Long.parseLong(playerIdStr);
      PlayerBo playerDom = blackListMgr.unLockPlayer(playerId);
      // 立即更新,后台实时看到结果
      playerMgr.updatePlayer(playerDom);
    }

    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 后台增加金币.
   * <p>
   * . serId long
   * </p>
   * .
   * <p>
   * . serName String
   * </p>
   * .
   * <p>
   * . playerId . long
   * </p>
   * .
   * <p>
   * . gold . long
   * </p>
   * .
   * <p>
   * . fishId . int 0:玩家身上金币,1:玩家保险箱金币
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes addGold(List<String> args) {
    int userId = Integer.parseInt(args.get(0));
    String userName = args.get(1);
    long playerId = Long.parseLong(args.get(2));
    long gold = Long.parseLong(args.get(3));
    int type = Integer.parseInt(args.get(4));

    String playerName = null;
    long superId = 0;
    if (type == 0) {
      Player player = playerMgr.getPlayer(playerId);

      if (player != null) {
        playerMgr.addGold(player, gold, LogReason.BACKEND_ADD_GOLD);
        playerMgr.updatePlayer(player.playerBo());
        playerName = player.getPlayerName();
        superId = player.getSuperId();
      } else {
        PlayerBo playerDom = playerMgr.selectPlayer(playerId);
        playerMgr.addGold(playerDom, gold, LogReason.BACKEND_ADD_GOLD);
        playerName = playerDom.getPlayerName();
        superId = playerDom.getSuperId();
        // 立即更新,后台实时看到结果
        playerMgr.updatePlayer(playerDom);
      }
    } else if (type == 1) {
      // playerMgr.addSafeGold(playerId, gold, LogReason.BACKEND_ADD_GOLD);
      // PlayerBo playerDom = playerMgr.selectPlayer(playerId);
      // playerName = playerDom.getPlayerName();
      // superId = playerDom.getSuperId();
    }

    DbLogService
        .log(new BackendAddGoldLog(userId, userName, playerId, superId, playerName, gold));
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 重载黑名单.
   * 
   * @param args ..
   * @return .
   */
  public HttpRes reloadBlackList(List<String> args) {
    blackListMgr.loadData();
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 重载白名单.
   * 
   * @param args ..
   * @return .
   */
  public HttpRes reloadWhiteList(List<String> args) {
    whiteListMgr.loadData();
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 重载福利.
   * 
   * @param args ..
   * @return .
   */
  public HttpRes reloadWelfare(List<String> args) {
    welfareDic.load();
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 重载系统配置.
   * 
   * @param args ..
   * @return .
   */
  public HttpRes reloadSysconfig(List<String> args) {
    sysconfigDic.load();
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 发送跑马灯公告.
   * <p>
   * . fishId . 1:跑马灯公告,2:聊天框系统公告,3:跑马灯和聊天框系统公告
   * </p>
   * .
   * <p>
   * . content . String
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes marqueeNotice(List<String> args) {
    int type = Integer.parseInt(args.get(0));
    String content = args.get(1);

    if (type == 1) {
      noticeMsgMgr.sendMarqueeNoticeMsg(content, NoticeType.OFFICIAL, Notice.DEFAULT_INTERVAL,
          Notice.DEFAULT_TIMES, Notice.DEFAULT_COLOR);
    } else if (type == 2) {
      chatMsgMgr.sendSysNoticeMsg(content);
    } else if (type == 3) {
      noticeMsgMgr.sendMarqueeNoticeMsg(content, NoticeType.OFFICIAL, Notice.DEFAULT_INTERVAL,
          Notice.DEFAULT_TIMES, Notice.DEFAULT_COLOR);
      chatMsgMgr.sendSysNoticeMsg(content);
    }

    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 发送喇叭.
   * <p>
   * . playerName . String
   * </p>
   * .
   * <p>
   * . content . String
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes sendHornMsg(List<String> args) {
    chatMsgMgr.sendHornMsg(args.get(0), args.get(1));
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 清除玩家游戏数据,防止玩家卡在游戏中不能退出来.
   * <p>
   * . playerId . long
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes clearGameData(List<String> args) {
    long playerId = Long.parseLong(args.get(0));
    if (playerId > 0) {
      eventMgr.post(new ClearGameDataEvent(playerId));
    } else {
      return new HttpRes(FAILURE, "清除玩家游戏数据的玩家id必须大于0", "");
    }

    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 关服 .
   * 
   * @param args ..
   * @return .
   */
  public HttpRes shutdownServer(List<String> args) {
    System.exit(0);
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 赠送玩家喇叭,玩家必须在线.
   * <p>
   * . playerId . long
   * </p>
   * .
   * <p>
   * . num . int 喇叭数量
   * </p>
   * .
   */
  public HttpRes giveHorn(List<String> args) {
    long playerId = Long.parseLong(args.get(0));
    int num = Integer.parseInt(args.get(1));
    Player player = playerMgr.getPlayer(playerId);
    if (player == null) {
      return new HttpRes(FAILURE, "玩家不在线不能赠送喇叭", "");
    }

    backpackMgr.addItem(player, 151, num, LogReason.BACKEND_ADD_ITEM);
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 开启房间.
   * <p>
   * . gameId . int
   * </p>
   * .
   * <p>
   * . roomId . int
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes startRoom(List<String> args) {
    int gameId = Integer.parseInt(args.get(0));
    int roomId = Integer.parseInt(args.get(1));
    Game game = Game.getGame(gameId);

    if (game == null || roomId < 0) {
      return new HttpRes(FAILURE, "游戏或房间参数不正确", "");
    }

    eventMgr.post(new StartRoomEvent(game, roomId));
    return new HttpRes(SUCCESS, "", "");
  }

  /**
   * 关闭房间.
   * <p>
   * . gameId . int
   * </p>
   * .
   * <p>
   * . roomId . int
   * </p>
   * .
   * 
   * @return .
   */
  public HttpRes shutDownRoom(List<String> args) {
    int gameId = Integer.parseInt(args.get(0));
    int roomId = Integer.parseInt(args.get(1));
    Game game = Game.getGame(gameId);

    if (game == null || roomId < 0) {
      return new HttpRes(FAILURE, "游戏或房间参数不正确", "");
    }

    eventMgr.post(new ShutdownRoomEvent(game, roomId));
    return new HttpRes(SUCCESS, "", "");
  }

}
